/**
 *Essa classe e responsavel pelas atualizacoes do agents rock n roll racing do tipo topdown view
 */
import core.gameelement.agent.perspective.*; 
import core.designpattern.mvc.*;
import core.*;

import util.*;
import flash.geom.*;
import flash.display.*;

import rocknrollracing.gameelement.agent.*;
import rocknrollracing.gameelement.*;
import rocknrollracing.phase.*;
 
class rocknrollracing.gameelement.agent.perspective.RocknRollRacingAgentTopDownViewPixel extends GameElementView  
{
	public function RocknRollRacingAgentTopDownViewPixel(canvas:MovieClip, model:Model)
	{
		super(canvas, model);
	}
	
	public function destroy():Void
	{
		super.destroy();
	}
	
	public function updateScreen():Void
	{
		var oldPositionX:Number = graphic._x; 
		var oldPositionY:Number = graphic._y; 
		var oldRotation:Number = graphic._rotation; 
		
		//RocknRollRacingAgentModel(model).getAceleration();
		var gameElements:Array = RocknRollRacingPhase(GameManager.getInstance().getActivePhase()).getGameElements();
		
		graphic._rotation = RocknRollRacingAgentModel(model).getRotation();
		graphic._x = model.getPosition().x;
		graphic._y = model.getPosition().y;
		
		var car:MovieClip = GameElementModel(model).getGraphic();
		//var movieclip:MovieClip = car["mCollisionArea"]; 			
		for(var i = 0; i < gameElements.length; i++)
		{
			// testa se e solido o movieclip e e se nao e o proprio carro :D
			if (gameElements[i].isSolid() && gameElements[i].getModel().getGraphic() != car)
			{				 								
				if (checkForCollision(car["mCollisionArea"], gameElements[i].getModel().getGraphic(),200))
				{
					var colision:Rectangle = checkForCollision(car["mCollisionArea"], gameElements[i].getModel().getGraphic(),255)
					Logger.trace("colision: " + colision,1);
					//if (checkForCollision(car["mCollisionArea"]["mCollisionFrontLeft"], gameElements[i].getModel().getGraphic(),255))
						//oldRotation += 5;
						
					//if (checkForCollision(car["mCollisionArea"]["mCollisionFrontRight"], gameElements[i].getModel().getGraphic(),255))
						//oldRotation -= 5;
					
					graphic._x = oldPositionX;		
					graphic._y = oldPositionY;
					graphic._rotation = oldRotation;
					
					model.setPosition( new Point(graphic._x, graphic._y));
					RocknRollRacingAgentModel(model).setRotation(oldRotation);
				}				
			}	
		}
				
	}
	
	public function checkForCollision(p_clip1:MovieClip,p_clip2:MovieClip,p_alphaTolerance:Number):Rectangle
	{
		
		// set up default params:
		if (p_alphaTolerance == undefined) { p_alphaTolerance = 255; }
		
		// get bounds:
		var bounds1:Object = p_clip1.getBounds(_root);
		var bounds2:Object = p_clip2.getBounds(_root);
		
		// rule out anything that we know can't collide:
		if (((bounds1.xMax < bounds2.xMin) || (bounds2.xMax < bounds1.xMin)) || ((bounds1.yMax < bounds2.yMin) || (bounds2.yMax < bounds1.yMin)) ) {
			return null;
		}
		
		// determine test area boundaries:
		var bounds:Object = {};
		bounds.xMin = Math.max(bounds1.xMin,bounds2.xMin);
		bounds.xMax = Math.min(bounds1.xMax,bounds2.xMax);
		bounds.yMin = Math.max(bounds1.yMin,bounds2.yMin);
		bounds.yMax = Math.min(bounds1.yMax,bounds2.yMax);
		
		// set up the image to use:
		var img:BitmapData = new BitmapData(bounds.xMax-bounds.xMin,bounds.yMax-bounds.yMin,false);
		
		// draw in the first image:
		var mat:Matrix = p_clip1.transform.concatenatedMatrix;
		mat.tx -= bounds.xMin;
		mat.ty -= bounds.yMin;
		img.draw(p_clip1,mat, new ColorTransform(1,1,1,1,255,-255,-255,p_alphaTolerance));
		
		// overlay the second image:
		mat = p_clip2.transform.concatenatedMatrix;
		mat.tx -= bounds.xMin;
		mat.ty -= bounds.yMin;
		img.draw(p_clip2,mat, new ColorTransform(1,1,1,1,255,255,255,p_alphaTolerance),"difference");
		
		// find the intersection:
		var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);
		
		// if there is no intersection, return null:
		if (intersection.width == 0) { return null; }
		
		// adjust the intersection to account for the bounds:
		intersection.x += bounds.xMin;
		intersection.y += bounds.yMin;
		
		return intersection;
	}
	
	
	public function toString():String
	{
		return "rocknrollracing.gameelement.agent.perspective.RocknRollRacingAgentTopDownView";
	}	
	
}